package com.lf.api.service.impl;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.lf.api.Constants;
import com.lf.api.po.dao.SubjectMapper;
import com.lf.api.po.domain.Subject;
import com.lf.api.util.IFConfig;
import com.lf.common.util.UuidUtils;

/**
 * 
 * Description: 音频下载
 * Copyright: Copyright (c)2012
 * Company: 小小书童
 * @author: 小小书童
 * @version: 1.0
 * Create at: 2016年10月6日 下午8:54:12
 *
 * Modification History:
 * Date Author Version Description
 * ---------------------------修改历史---------------------------------------
 * 2016年10月6日 小小书童 1.0
 */
@Service
public class DownloadVoiceServiceImpl {
	private static Logger log = LoggerFactory.getLogger(DownloadVoiceServiceImpl.class);
	@Autowired
	SubjectMapper subjectMapper;
	/***
	 * 
	 * Description:
	 * Date:2016年10月6日
	 * @author 小小书童
	 * @return List<Subject>
	 */
	public void downloadSubjectVoice(){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("audioType", '0');
		List<Subject> subjects = subjectMapper.querySubjectByList(params);
		downloadVoice(subjects);
	}
	
	public void downloadVoice(List<Subject> subjects){
		if(CollectionUtils.isEmpty(subjects)){
			return;
		}
		String baseAudioUrl = IFConfig.getValue(Constants.VOICE_PATH, "");
		for(Subject subject : subjects){
			if(null != subject && subject.getAudioType().intValue() == 0 
					&& StringUtils.isNotEmpty(subject.getAudioUrl())){
				String fileName = downloadFile(subject.getAudioUrl(), baseAudioUrl);
				if(StringUtils.isNotEmpty(fileName)){
					subject.setAudioType((short)1);
					subject.setAudioUrl(fileName);
					subjectMapper.update(subject);
				}
			}
		}
	}
	
	public String downloadFile(String remoteFilePath, String basePath) {
		URL urlfile = null;
		HttpURLConnection httpUrl = null;
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		String filePath = null;
		try {
			urlfile = new URL(remoteFilePath);
			httpUrl = (HttpURLConnection) urlfile.openConnection();
			httpUrl.connect();
			//获取文件名
			String fileName = httpUrl.getHeaderField("filename");
			String extName = null;
			if(StringUtils.isNotEmpty(fileName)){
				extName = fileName.substring(fileName.indexOf("."));
			}
			fileName = UuidUtils.compressedUuid() + extName;
			Calendar c = Calendar.getInstance();
			String path = File.separator + c.get(Calendar.YEAR) + File.separator + c.get(Calendar.MONTH);
			File pathFile = new File(basePath + path);
			if(!pathFile.exists()){
				pathFile.mkdirs();
			}
			File file = new File(pathFile,  fileName);
			if(!file.exists()){
				file.createNewFile();
			}
			filePath = path + File.separator + fileName;
			bis = new BufferedInputStream(httpUrl.getInputStream());
			bos = new BufferedOutputStream(new FileOutputStream(file));
			byte[] byteArr = new byte[bis.available()];
			bis.read(byteArr);
			bos.write(byteArr);
			bos.flush();
			bis.close();
			httpUrl.disconnect();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				bis.close();
				bos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return filePath;
	}
}
